package com.team.ssm_item_lj.controller;


import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/vue_userinfo")
public class Vue_UserInfo_Servlet extends HttpServlet {

    //定义一个List集合，用来存储用户信息
    static List<UserInfo> list=new ArrayList<>();
    String ms="{'code':'500','ms':'err'}";
    static {
        for (int i = 0; i < 10; i++) {
            UserInfo  ui=new UserInfo();
            ui.setUserId(1000+i+"");
            ui.setUserName("admin"+i);
            ui.setUserPhone("15959241188");
            ui.setUserMail("goodshow@qq.com");

            list.add(ui);
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String op=req.getParameter("op");
        if(op.equals("ui_list")){
            System.out.println("----------请求后出台---- ui_list--------");
            String jsonString = JSON.toJSONString(list);//将数据库中的查询List集合转化为json字符串
            resp.setContentType("text/html;charset=UTF-8");//设置响应数据的类型
            resp.getWriter().write(jsonString);

        }
        if(op.equals("ui_save")){
            System.out.println("----------请求后出台---- ui_save--------");
            //获取请求参数
            ServletInputStream inputStream = req.getInputStream();
            UserInfo o = JSON.parseObject(inputStream, UserInfo.class);//将请求参数转化为UserInfo对象

            if(list.add(o)){
                ms="[{'code':200,'ms':'增加成功'}]";
                resp.setContentType("text/html;charset=UTF-8");//设置响应数据的类型
                resp.getWriter().write(ms);
            }

        }
        if(op.equals("ui_upadte")){
            System.out.println("----------请求后出台---- ui_upadte--------");
            //获取请求参数
            //获取请求参数
            ServletInputStream inputStream = req.getInputStream();
            UserInfo ui = JSON.parseObject(inputStream, UserInfo.class);//将请求参数转化为UserInfo对象

            for (int i = 0; i <list.size() ; i++) {
                UserInfo userInfo = list.get(i);

                if(userInfo.getUserId().equals(ui.userId)){
                    userInfo.setUserId(ui.userId);
                    userInfo.setUserName(ui.getUserName());
                    userInfo.setUserPhone(ui.getUserPhone());
                    userInfo.setUserMail(ui.getUserMail());
                    ms="{'code':200,'ms':'ok'}";
                }
            }
            String jsonString = JSON.toJSONString(ui);
            resp.setContentType("text/html;charset=UTF-8");//设置响应数据的类型
            resp.getWriter().write(jsonString);

        }
        if(op.equals("ui_del")){
            System.out.println("----------请求后出台---- ui_del--------");
            //获取请求参数
            ServletInputStream inputStream = req.getInputStream();
            UserInfo ui = JSON.parseObject(inputStream, UserInfo.class);//将请求参数转化为UserInfo对象

            for (int i = 0; i <list.size() ; i++) {
                UserInfo userInfo = list.get(i);

                if(userInfo.getUserId().equals(ui.userId)){
                    list.remove(userInfo);
                    ms="{'code':'200','ms':'ok'}";
                    resp.setContentType("text/html;charset=UTF-8");//设置响应数据的类型
                    resp.getWriter().write(ms);
                }
            }



        }


    }


    }

     class UserInfo {

         String userId;
         String userName;
         String userPhone;
         String userMail;

         @Override
         public String toString() {
             return "UserInfo{" +
                     "userId='" + userId + '\'' +
                     ", userName='" + userName + '\'' +
                     ", userPhone='" + userPhone + '\'' +
                     ", userMail='" + userMail + '\'' +
                     '}';
         }

         public UserInfo() {
       }

       public UserInfo(String userId, String userName, String userPhone, String userMail) {
           this.userId = userId;
           this.userName = userName;
           this.userPhone = userPhone;
           this.userMail = userMail;
       }

       public String getUserId() {
           return userId;
       }

       public void setUserId(String userId) {
           this.userId = userId;
       }

       public String getUserName() {
           return userName;
       }

       public void setUserName(String userName) {
           this.userName = userName;
       }

       public String getUserPhone() {
           return userPhone;
       }

       public void setUserPhone(String userPhone) {
           this.userPhone = userPhone;
       }

       public String getUserMail() {
           return userMail;
       }

       public void setUserMail(String userMail) {
           this.userMail = userMail;
       }
   }
